package com.hllxd.hism.star.config.mvc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import javax.servlet.Filter;

/**
 * @author lixiyong01
 * @date 2020/11/01
 */
@Configuration
public class WebConfiguration extends WebMvcConfigurerAdapter {

    @Autowired
    private AtomicInterceptor atomicInterceptor;


//    @Value("${passport.state}")
//    private String passportState;


    @Value("${permission.state}")
    private String permissionState;

//    @Autowired
//    private PassportInterceptor passportInterceptor;

    @Autowired
    private PermissionInterceptor permissionInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 认证拦截 disable，enable
        String validCode = "enable";



        // 基础监控拦截-打印日志
        registry.addInterceptor(atomicInterceptor)
                .addPathPatterns("/star/api/**")
                .addPathPatterns("/star/apiwx/**")
                .addPathPatterns("/star/download/**");


        // 针对小程序进行拦截
//        if (passportState.equals(validCode)) {
//            registry.addInterceptor(passportInterceptor)
//                    .addPathPatterns("/star/apiwx/**");
//        }

        // 权限api接口进行拦截（体验不好）
        if (permissionState.equals(validCode)) {
            registry.addInterceptor(permissionInterceptor)
                    .addPathPatterns("/star/api/**")
                    .excludePathPatterns("/star/api/basic-service/v1/aaa/aaa.do");
        }
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/");
        super.addResourceHandlers(registry);
    }

    @Bean
    public InternalResourceViewResolver getInternalResourceViewResolver() {
        //return new InternalResourceViewResolver();
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setSuffix(".html");
        return resolver;
    }

    @Bean
    public Filter characterEncodingFilter() {
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding("UTF-8");
        characterEncodingFilter.setForceEncoding(true);
        return characterEncodingFilter;
    }
}
